<?php

declare(strict_types=1);
/**
 * This file is part of MoChat.
 * @link     https://mo.chat
 * @document https://mochat.wiki
 * @contact  group@mo.chat
 * @license  https://github.com/mochat-cloud/mochat/blob/master/LICENSE
 */
namespace MoChat\Plugin\ContactStage\Service;

use MoChat\Framework\Service\AbstractService;
use MoChat\Plugin\ContactStage\Contract\ContactStageEmployeeContract;
use MoChat\Plugin\ContactStage\Model\ContactStageEmployee;

class ContactStageEmployeeService extends AbstractService implements ContactStageEmployeeContract
{
    /**
     * @var ContactStageEmployee
     */
    protected $model;

    /**
     * 查询单条 - 根据客户id和员工id,企业id.
     * @param int $contactId ID
     * @param int $employeeId ID
     * @param int $corpId ID
     * @param array|string[] $columns 查询字段
     * @return array 数组
     */
    public function getContactStageEmployeeByOtherId(int $contactId, int $employeeId, int $corpId, array $columns = ['*']): array
    {
        $res = $this->model::query()
            ->where('contact_id', $contactId)
            ->where('employee_id', $employeeId)
            ->where('corp_id', $corpId)
            ->first($columns);

        if (empty($res)) {
            return [];
        }

        return $res->toArray();
    }

    /**
     * 添加单条
     * @param array $data 添加的数据
     * @return int 自增ID
     */
    public function createContactStageEmployee(array $data): int
    {
        return $this->model->createOne($data);
    }

    /**
     * 修改单条 - 根据ID.
     * @param int $id id
     * @param array $data 修改数据
     * @return int 修改条数
     */
    public function updateContactStageEmployeeById(int $id, array $data): int
    {
        return $this->model->updateOneById($id, $data);
    }

    /**
     * 查询多条 - 根据客户id和员工id,企业id.
     * @param int $contactId ID
     * @param array $employeeIds ID
     * @param int $corpId ID
     * @param array|string[] $columns 查询字段
     * @return array 数组
     */
    public function getContactStageEmployeesByOtherId(int $contactId, array $employeeIds, int $corpId, array $columns = ['*']): array
    {
        $res = $this->model::query()
            ->where('contact_id', $contactId)
            ->whereIn('employee_id', $employeeIds)
            ->where('corp_id', $corpId)
            ->get($columns);

        if (empty($res)) {
            return [];
        }

        return $res->toArray();
    }

    /**
     * 查询多条 - 根据阶段id.
     * @param array|string[] $columns 查询字段
     * @return array 数组
     */
    public function getContactStageEmployeeByStageId(int $stageId, array $columns = ['*']): array
    {
        $res = $this->model::query()
            ->where('stage_id', $stageId)
            ->get($columns);

        if (empty($res)) {
            return [];
        }

        return $res->toArray();
    }

    /**
     * 查询多条 - 根据企业id.
     * @param array|string[] $columns 查询字段
     * @return array 数组
     */
    public function getContactStageEmployeeByCorpId(int $corpId, array $columns = ['*']): array
    {
        $res = $this->model::query()
            ->where('corp_id', $corpId)
            ->get($columns);

        if (empty($res)) {
            return [];
        }

        return $res->toArray();
    }

    /**
     * 删除多条-根据客户id.
     */
    public function deleteContactStageEmployeeByContactIdEmployeeId(int $contactId, int $employeeId): int
    {
        return $this->model::query()
            ->where('contact_id', $contactId)
            ->where('employee_id', $employeeId)
            ->delete();
    }

    /**
     * 查询多条 - 根据阶段id、员工ID.
     * @param array|string[] $columns 查询字段
     * @return array 数组
     */
    public function getContactStageEmployeeByStageIdEmployeeId(int $stageId, int $employeeId, array $columns = ['*']): array
    {
        $res = $this->model::query()
            ->where('stage_id', $stageId)
            ->where('employeeId', $employeeId)
            ->get($columns);

        if (empty($res)) {
            return [];
        }

        return $res->toArray();
    }
}
